package com.project.webapp.action.backend.system;

import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.project.dao.bean.Role;
import com.project.dao.bean.UserInfo;
import com.project.service.backend.basic.RoleService;
import com.project.service.backend.basic.UserInfoService;
import com.project.webapp.action.BaseAction;

/**
 * 后台 用户管理 Action 
 * @author Sn
 *
 */
public class UserAction extends BaseAction {

	/**
	 * 序列化版本号
	 */
	private static final long serialVersionUID = 1L;
	private static final String ADD_MENU = "addUser";
	private String operateResult = "";
	private UserInfoService userService;
	private UserInfo userInfo;
	List<UserInfo> userList;
	List<Role> roleList;
	private RoleService roleService;
	
	@Override
	public String execute() throws Exception 
	{
		userInfo = new UserInfo();
		if(userList!=null && userList.size()>0)
		{
			userList.clear();
		}
		return this.query();
	}
	
	/**
	 * 验证用户名
	 * @throws Exception 
	 */
	public void check() throws Exception
	{
		boolean check = false;
		check = userService.check(userInfo.getLoginName());
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter pw = response.getWriter();
		if(check)
		{			
			pw.write("0");//用户名可以使用
			
		}else
		{
			pw.write("1");//用户名已经被使用
		}
		pw.flush();
		pw.close();
	}
	
	public String showAdd() 
	{
		userInfo = new UserInfo();
		try 
		{
			roleList = roleService.findRole();
			
		} catch (Exception e) 
		{
			e.printStackTrace();
		}
        operateResult = "";
        return SUCCESS;
    }

    /**
     * 
     * <pre>
     * 查询
     * </pre>
     *
     * @return
     */
    public String query() 
    {
        try 
        {
            operateResult = "";
            userList = userService.findUserBypg(userInfo);
            
        } catch (Exception e) 
        {
            e.printStackTrace();
        }

        return SUCCESS;
    }
    
    /**
     * 
     * <pre>
     * 删除
     * </pre>
     *
     * @return
     */
    public String delete() 
    {
    	System.out.println("delete~");
        HttpServletRequest req = ServletActionContext.getRequest();
        String userId = req.getParameter("id");
        int id = Integer.parseInt(userId);
        try 
        {
        	userInfo.setId(id);
            
        	userService.delete(userInfo);
            userList = userService.findUserBypg(userInfo);
            operateResult = "suc";
            
        } catch (Exception e) 
        {
            e.printStackTrace();
            operateResult = "fail";
        }
        return SUCCESS;
    }
    
    /**
     * 
     * <pre>
     * 编辑
     * </pre>
     *
     * @return
     * @throws Exception 
     */
    public String edit() throws Exception 
    {
        HttpServletRequest req = ServletActionContext.getRequest();
        if (userInfo == null) 
        {
        	userInfo = new UserInfo();
        }
        operateResult = "";
        String UesrId = req.getParameter("id");
        int id = Integer.parseInt(UesrId);
        userInfo.setId(id);
        userInfo = userService.getUserById(userInfo);
        roleList = roleService.findRole();
        return SUCCESS;
    }
    
    /**
     * 
     * <pre>
     * 保存
     * </pre>
     *
     * @return
     */
    public String save() 
    {
    	try 
    	{
    		userService.save(userInfo);
    		operateResult = "suc";
    	} catch (Exception e) 
    	{
    		e.printStackTrace();
    		operateResult = "fail";
    	}
    	userInfo = new UserInfo();
    	return ADD_MENU;
    }
    
    public String update() 
    {
    	try 
    	{
    		userService.update(userInfo);
    		operateResult = "suc";
    		
    	} catch (Exception e) 
    	{
    		e.printStackTrace();
    		operateResult = "fail";
    	}
    	userInfo = new UserInfo();
    	return ADD_MENU;
    }

	public static String getAddMenu() 
	{
		return ADD_MENU;
	}
	
	public String getOperateResult() {
		return operateResult;
	}

	public void setOperateResult(String operateResult) {
		this.operateResult = operateResult;
	}

	public UserInfoService getUserService() {
		return userService;
	}

	public void setUserService(UserInfoService userService) {
		this.userService = userService;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public List<UserInfo> getUserList() {
		return userList;
	}

	public void setUserList(List<UserInfo> userList) {
		this.userList = userList;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

}
